package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.ZytologyDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.ZytologyTBL;

/**
 * Session Bean implementation class ZytologyDAO
 * 
 * @author Patrick kendzo Email: pkendzo@msn.com
 */

@Stateless
@Local(
{ ZytologyDAOInt.class })
public class ZytologyDAO implements ZytologyDAOInt
{

    @PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em = null;

    public EntityManager getEntityManager()
    {
	return em;

    }

    private boolean permission;

    /**
     * Default constructor.
     */
    public ZytologyDAO()
    {

    }

    @Override
    public void delete(Long todo)
    {
	ZytologyTBL item = em.find(ZytologyTBL.class, todo);

	CustomerTBL cust = item.getCustomer();

	cust.getZytology().remove(item);

	em.remove(item);
	em.flush();

    }

    @Override
    public void persist(ZytologyTBL todo)
    {
	em.persist(todo);
	em.flush();

    }

    @Override
    public ZytologyTBL retrieveZytologyItemByID(Long id)
    {
	return em.find(ZytologyTBL.class, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ZytologyTBL> retrieveZytologyItemsByCustomer(String matricule)
    {
	try
	{
	    return (List<ZytologyTBL>) em.createQuery(
		    "select a from ZytologyTBL a where a.customer = '"
			    + matricule + "'").getResultList();
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "Could not retrieve Zytoloy item for this Customer", e);
	}
    }

    @Override
    public void update(ZytologyTBL todo)
    {
	em.merge(todo);
	em.flush();
/*
	Object[] tmp = getZytoItemSortedByNumber(todo.getYear()).toArray();
	// ZytologyTBL[] eintraege = new ZytologyTBL[tmp.length];

	for (int i = 0; i < tmp.length; i++)
	{
	    ((ZytologyTBL) tmp[i]).setZytoNumber(i + 1);
	}
*/
    }

    @Override
    public int getActualZytoNumberIndex(String year)
    {
	try
	{

	    Integer result = (Integer) em.createQuery(
		    "select max(z.zytoNumber) from ZytologyTBL z where z.year ='"
			    + year + "'").getSingleResult();

	    return result.intValue();
	}
	catch (Exception e)
	{
	    /*
	     * throw new RuntimeException("OrdonanceTBL is empty", e);
	     */
	    return 0;
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ZytologyTBL> getZytoItemWithoutRESULT()
    {
	try
	{
	    return (List<ZytologyTBL>) em
		    .createQuery(
			    "SELECT z FROM ZytologyTBL z where z.ergebnis is NULL OR z.ergebnis =\'\' order by z.year, z.zytoNumber")
		    .getResultList();
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "Could not retrieve Zytology items without Results", e);
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ZytologyTBL> getZytoItemsOfINTERVAL(String fromDatum,
	    String toDatum)
    {
	try
	{
	    return (List<ZytologyTBL>) em.createQuery(
		    "SELECT a FROM ZytologyTBL a where  STR_TO_DATE(a.datum,'%d.%m.%Y') between "
			    + "STR_TO_DATE('" + fromDatum
			    + "','%d.%m.%Y') and  STR_TO_DATE('" + toDatum
			    + "','%d.%m.%Y') order by a.year, a.zytoNumber")
		    .getResultList();
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "Could not retrieve Zytology items for this Interval", e);
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ZytologyTBL> getZytoItemSortedByNumber(String year)
    {
	try
	{
	    return (List<ZytologyTBL>) em.createQuery(
		    "SELECT z FROM ZytologyTBL z where year = '" + year
			    + "' order by STR_TO_DATE(z.datum,'%d.%m.%Y') ")
		    .getResultList();
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "Could not sorted Zytology items for this year", e);
	}
    }

    @Override
    public boolean getPermission()
    {
	return this.permission;
    }

    @Override
    public void setPermission(boolean value)
    {
	this.permission = value;

    }

}
